package com.sunkyc.controller.system;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.sunkyc.commons.annotation.ReqLog;
import com.sunkyc.commons.entity.R;
import com.sunkyc.commons.enums.DataScopeType;
import com.sunkyc.commons.exception.CustomerException;
import com.sunkyc.framework.system.entity.SysRole;
import com.sunkyc.framework.system.service.SysRoleService;
import com.sunkyc.commons.entity.PageQueryVo;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.sunkyc.commons.entity.TablePageData;
import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author sunkyc
 * @since 2022-05-31
 */
@RestController
@RequestMapping("/sysRole")
@RequiredArgsConstructor
@Api(tags = "角色信息管理")
public class SysRoleController {
    private final SysRoleService baseService;

    @PostMapping
    @ApiOperation("角色添加")
    @SaCheckPermission("system-role-add")
    @ReqLog
    public R<Void> addRole(@RequestBody @Validated SysRole sysRole){
        // 判断角色名称是否唯一
        if(baseService.checkNameUnique(sysRole)){
            throw new CustomerException("添加失败:角色名称 "+sysRole.getRoleName()+" 已经存在");
        }
        // 判断角色标识是否唯一
        if(baseService.checkKeyUnique(sysRole)){
            throw new CustomerException("添加失败:权限标识 "+sysRole.getRoleKey()+" 已经存在");
        }
        return R.result(baseService.addRole(sysRole));
    }

    @PutMapping
    @ApiOperation("角色修改")
    @SaCheckPermission("system-role-update")
    @ReqLog
    public R<Void> updateRole(@RequestBody @Validated SysRole sysRole){
        // 判断角色名称是否唯一
        if(baseService.checkNameUnique(sysRole)){
            throw new CustomerException("添加失败:角色名称 "+sysRole.getRoleName()+" 已经存在");
        }
        // 判断角色标识是否唯一
        if(baseService.checkKeyUnique(sysRole)){
            throw new CustomerException("添加失败:权限标识 "+sysRole.getRoleKey()+" 已经存在");
        }
        return R.result(baseService.updateRole(sysRole));
    }



    @GetMapping("/list")
    @ApiOperation("角色列表")
    @SaCheckPermission("system-role-list")
    public R<TablePageData<SysRole>> queryList(PageQueryVo pageQueryVo, SysRole sysRole){
        return R.ok(baseService.queryList(pageQueryVo, sysRole));
    }



    @DeleteMapping("/{ids}")
    @ApiOperation("角色删除")
    @SaCheckPermission("system-role-delete")
    @ReqLog
    @ApiImplicitParams({@ApiImplicitParam(name = "ids",value = "需要删除角色id或者id数组")})
    public R<Void> delByRoleids(@PathVariable("ids") Long[] ids){
        // 查询角色是否被分配
        for (Long id : ids) {
            if(baseService.checkedRoleUsedByRoleId(id)) throw new CustomerException("删除失败，角色已经被分配");
        }
        return R.result(baseService.delByRoleids(ids));
    }


    @GetMapping("/{roleId}")
    @ApiOperation("角色查询")
    @SaCheckPermission("system-role-query")
    public R<SysRole> queryById(@PathVariable("roleId") Long roleId){
        return R.ok(baseService.queryById(roleId));
    }



    @PutMapping("/updateRoleDataScope")
    @ApiOperation("角色数据权限修改")
    @ReqLog
    @SaCheckPermission("system-role-data-scope-update")
    public R<Void> updateRoleDataScope(@RequestBody  SysRole sysRole){
        if(DataScopeType.CUSTOM.getCode().equals(sysRole.getDataScope()) && sysRole.getDeptIds().size()<=0){
            throw new CustomerException("设置数据权限失败: 请选择对应组织");
        }
        return R.result(baseService.updateRoleDataScope(sysRole));
    }



    @GetMapping("/all")
    @ApiOperation("获取所有角色列表信息")
    @SaCheckPermission("system-role-all-list")
    public R<List<SysRole>> queryAll(){
        return R.ok(baseService.queryAll());
    }



}
